﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using App.Core.Extensions;
using App.Data;

public partial class Pages_Admin_ArticleList : BasePage
{
    private int _ID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            CheckAndDeleteData();
            BindNewsList(1);
        }
    }
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void CheckAndDeleteData()
    {
        if (String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.DELETE), "True", false) == 0)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            var news = context.Articles.FirstOrDefault(P => P.ID == _ID);
            if (news == null)
                WebUtil.ShowMessageBox(divMessage, "Sorry! Requested Article was not found for delete.", true);
            else
            {
                news.IsDeleted = true;
                news.Modified = DateTime.Now;
                news.ModifiedBy = SessionCache.CurrentUser.ID;
                context.SaveChanges();
                WebUtil.ShowMessageBox(divMessage, "Article Deleted Successfully.", true);
            }
        }
    }

    
    protected void BindNewsList(int pageNumber)
    {
        int totalRecord = 0;
        UtilityDAO dao = new UtilityDAO();
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_ARTICLE_LIST, null, pageNumber, int.MaxValue, out totalRecord);
        ucArticleList.DataSource = ds.Tables[0];
        ucArticleList.EditLink = AppConstants.Pages.ARTICLE_EDIT + "?" + AppConstants.QueryString.ID + "={0}";
        ucArticleList.DeleteLink = Request.Url.AbsolutePath + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.DELETE + "=True";
        ucArticleList.DataBind();
    }
}